<?php
/**

 */
class Admin_NewsController extends App_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
    	$request=$this->getRequest();  

		$form = new Form_NewsForm();
		$this->view->form = $form;

		$start = $request->getParam('page',1);
		$item_per_page = 20;		
		
		$user_id = $this->getUserId();
		$user_role = 'admin';

		$model_client = Model_UserFactory::create($user_role);

		
		if($request->isPost()){
			$data = $request->getParams();
			if ($data['submit']=='Add News'){
				$content=$data['news_content'];
				$dpt_id=$data['dpt_id'];
				$country=$data['country'];
				$city=$data['city'];
				$state=$data['state'];
				$max_display_time=$data['max_display_time'];
				$from_date=$data['from_date'];
				$end_date=$data['end_date'];
				
				$id = $model_client->addNews($data);
    			if ($id)
				{
				   $this->view->success_msg="News has been added Successfully!";
				}else{
				   $this->view->error_msg="Sorry, add news failed.";					
				}				
				
			}else if ($data['submit']=='Update News'){
				$news_id=$data['news_id'];
				$content=$data['news_content'];
				$update = $model_client->updateNews($news_id,$data);
    			if ($update)
				{
				   $this->view->success_msg="News has been updated Successfully!";
				}else{
				   $this->view->error_msg="Sorry, update news failed.";					
				}				
			}
			
		}else{
			$act = $request->getParam('act');
			if (isset($act)and($act=='del')){
				$id = $request->getParam('id');		
				$result = $model_client->deleteNews($id);
				if ($result)
				{
				   $this->view->success_msg="News has been deleted!";
				}else{
				   $this->view->error_msg="Sorry, delete news failed.";					
				}		
			}			
		}
				
		$start_num = ($start-1)*$item_per_page;
		$this->view->start=$start_num;
		
		$all_news = $model_client->getAllNews($start_num,$item_per_page);
		
		$this->view->all_news=$all_news['data'];

		Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination.phtml');
    	
    	$paginator = Zend_Paginator::factory($all_news['total']);
    	$paginator->setItemCountPerPage($item_per_page); //num per page
    	$paginator->setCurrentPageNumber($start);
    	$this->view->paginator = $paginator;	

    	//Zend_Debug::dump($this->view->paginator);exit;
		
    }
    
    
	public function logoutAction()
	{
		$session = $this->getSession();
		$session->unsetAll();

		$this->_redirect('/');
	}

    
}